package com.j.lemon.learn.thread.future;

import java.util.Random;
import java.util.concurrent.*;

public class FutureTest {
    private static final int NUMBER=10000000;
    private static final int THREAD_COUNT=4;
    private static  ExecutorService ex = Executors.newFixedThreadPool(THREAD_COUNT);
    public static Random random = new Random(123);
    private static final ThreadLocal<Random> THREAD_LOCAL = ThreadLocal.withInitial(() -> new Random(123));

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Future<Long>[] future = new Future[THREAD_COUNT];
        for(int i=0;i<THREAD_COUNT;i++){
            future[i] = ex.submit(new Demo(1));
        }
        long totalTime=0;
        for(int i = 0;i<THREAD_COUNT;i++){
            totalTime+=future[i].get();
        }
        System.out.println("一共:"+totalTime+"ms");
    }

    public static class Demo implements Callable{
        int modal=0;

        public Demo(int modal) {
            this.modal = modal;
        }

        public Random getRandom(){
            if(modal==0){
                return random;
            }else{
                return THREAD_LOCAL.get();
            }
        }

        @Override
        public Object call() {
            long a = System.currentTimeMillis();
            for(int i=0;i<NUMBER;i++){
                getRandom().nextInt();
            }
            long b = System.currentTimeMillis();
            System.out.println(Thread.currentThread().getName()+":"+(b-a));
            return b-a;
        }
    }
}
